<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Exception;
/*
 * 文章管理控制器
 */
 class ContentController extends CommonController {
 	public function add (){//添加文章
 		if($_POST){
		 	if(!isset($_POST['title']) || !$_POST['title']){
		 		print_r($_POST);
		 		return show(0, '标题不存在');
			}
			if(!isset($_POST['small_title']) || !$_POST['small_title']){
		 		return show(0, '短标题不存在');
			}
			if(!isset($_POST['catid']) || !$_POST['catid']){
		 		return show(0, '文章栏目不存在');
			}
			if(!isset($_POST['keywords']) || !$_POST['keywords']){
		 		return show(0, '关键字不存在');
			}
			if(!isset($_POST['content']) || !$_POST['content']){
		 		return show(0, '内容不可为空');
			}
			if($_POST['news_id']){
				return $this->save($_POST);
			}
			
			$newsId = D('News')->insert($_POST);
			if($newsId){
				$newsContentData['content'] = $_POST['content'];
				$newsContentData['news_id'] = $newsId;
				$cId = D('NewsContent')->insert($newsContentData);
				if($cId){
					return show(1,"新增成功");
				}else{
					return show(1, '主表插入成功，副表插入失败');
				}
			}else{
				return show(0,"新增失败");
			}
	 	}else{
	 		$webSiteMenu = D('Menu')->getBarMenus();
			$titleFontColor = C('TITLE_FONT_COLOR');
			$copyFrom = C('COPY_FROM');
			$this->assign('webSiteMenu',$webSiteMenu);
			$this->assign('titleFontColor',$titleFontColor);
			$this->assign('copyFrom',$copyFrom);
	 		$this->display();
	 		
	 	}
 	}
	public function index () {//文章列表
		$conds = array();
		$title = $_GET['title'];
		if($title){
			$conds['title'] = $title;
		}
		if($_GET['catid']){
			$conds['catid'] = intval($_GET['catid']);
			 $this->assign('catid',$conds['catid']);
		}
		
		$page = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
		$pageSize = 8;//每页显示数量
		$conds['status'] = array('neq',-1);
		$news = D('News')->getNews($conds,$page,$pageSize);
		$count = D('News')->getNewsCount($conds);
		$positions = D('Position')->getNormalPositions();
		$res = new \Think\Page($count,$pageSize);
		$pagers = $res->show();
		$this->assign('positions',$positions);
		$this->assign('pagers',$pagers);
		$this->assign('news',$news);
	
		$this->assign('webSiteMenu',D('Menu')->getBarMenus());
		$this->display();
	}
	
	public function edit () {//文章编辑
		$newsId = $_GET['id'];
		if(!$newsId){
			//文章id不存在执行跳转
			$this ->rediret('/admin.php?c=content');
		}
		$news = D('News')->getNewsById($newsId);
		if(!$news){
			//文章不存在执行跳转
			$this ->rediret('/admin.php?c=content');
		}
		$newsContent = D('NewsContent')->getNewsById($newsId);
		if($newsContent){
			$news['content'] = $newsContent['content'];
		}
		$status = $_GET['status'];
		$this->assign('webSiteMenu',D('Menu')->getBarMenus());
		$this->assign('titleFontColor',C('TITLE_FONT_COLOR'));
		$this->assign('copyFrom',C('COPY_FROM'));
		$this->assign('news',$news);
		$this->assign('status',$status);
		$this->display();
		
	}
	
	public function save ($data) {//保存更新
		$news_id = $data['news_id'];
		unset($data['news_id']);
		try{
		    $id = D('News')->updataById ($news_id,$data);
			$newsContentData['content'] = $data['content'];
			$condId = D('NewsContent')->updataById($news_id,$newsContentData);
			if($id===false || $condId===false){
				return show(0, '更新失败');
			}
			return show(1, '更新成功');
		}catch(Exception $e){
			return show(0, $e->message);
		}
	}
	
	public function del () {//删除 --改变文章状态为-1，并非彻底删除数据
		try{
			if($_POST){
				$id=$_POST['id'];
				$status=$_POST['status'];
				$res=D('News')->updataStatusById($id,$status);
				if($res){
					return show(1, '操作成功！');
				}else{
					return show(0, '操作失败！');
				}
			}
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
		return show(0, '没有提交的数据');
	}
	
	public function listorder () {//文章排序
		$listoder=$_POST['listorder'];
		$jumpUrl=$_SERVER['HTTP_REFERER'];
		$errors=array();
		if($listoder){
			try{
			foreach($listoder as $newsId=>$v){
				$id=D('News')->updataNewsListorderById($newsId,$v);
				if($id===false){
					$errors[]=$newsId;
				}
			}
			}catch(Exception $e){
				return show(0, $e->getMessage(),array('jump_url'=>$jumpUrl));
			}
			if($errors){
				return show(0, '排序失败-'.implode(',', $errors),array('jump_url'=>$jumpUrl));
			}
			return show(1, '排序成功',array('jump_url'=>$jumpUrl));
		}
		return show(0, '排序数据失败',array('jump_url'=>$jumpUrl));
	}
	
	public function push () {//文章推送
		$jumpUrl = $_SERVER['HTTP_REFERER'];
		$positionId = $_POST['position_id'];
		$newsId = $_POST['push'];
		if(!$newsId || !is_array($newsId)){
			return show(0, '请选择推荐文章ID进行推荐');
		}
		if(!$positionId){
			return show(0, '请选择推荐位');
		}
		try{
			$news = D('News')->getNewsByNewsIdIn($newsId);
			if(!$news){
				return show(0, "没有相关数据");
			}
			foreach($news as $new){
				$data = array(
					'position_id' => $positionId,
					'title' => $new['title'],
					'thumb' => $new['thumb'],
					'news_id' => $new['news_id'],
					'status' =>1,
					'create_time' => $new['create_time'],
				);
				$position =  D('PositionContent')->insert($data);
			}
		}catch(Exception  $e){
			return show(0,$e->getMessage());
		}
		return show(1,"推荐成功",array('jump_url'=>$jumpUrl));
	}
 }